#include <iostream>
#include <map>
#include <string>
using namespace std;

class Observer 
{
public:
	virtual ~Observer() {}
	virtual void update(int temperature)=0;
};

class HmiObserver: public Observer 
{
public:
	void update(int temperature) 
	{
		cout << "show the current temperature in hmi: " << temperature <<endl;
	}
};

class DashboardObserver: public Observer 
{
public:
	void update(int temperature) 
	{
		cout << "show the trend of the temperature in dashboard: " << temperature << endl;
	}
};

class MessageObserver: public Observer 
{
public:
	void update(int temperature) 
	{
		cout << "send messages about temperature change: " << temperature << endl;
	}
};

class Subject 
{
public:
	virtual ~Subject() {}
	virtual void attach(string id, Observer* observer)=0;
	virtual void detach(string id)=0;
	virtual void notify(int temperature)=0;
};

class TemperatureSubject: public Subject 
{
private:
	map<string, Observer*> observers;
public:
	void attach(string id, Observer* observer) 
	{
		observers[id] = observer;
	}
	void detach(string id) 
	{
		map<string, Observer*>::iterator iter;
		iter = observers.find(id);
		if(iter != observers.end()) observers.erase(iter);
	}
	void notify(int temperature) 
	{
		map<string, Observer*>::iterator iter;
		for(iter = observers.begin(); iter != observers.end(); iter++)
			iter->second->update(temperature);
	}
};

int main() 
{
	Subject* subject = new TemperatureSubject();
	subject->attach("HmiObserver", new HmiObserver());
	subject->attach("DashboardObserver", new DashboardObserver());
	subject->attach("MessageObserver", new MessageObserver());

	cout << ">>> The temperature update to 50" << endl;
	subject->notify(50);
	cout << ">>> The temperature update to 100" << endl;
	subject->notify(100);
	return 0;
}
